package com.loongtech.bi.action.bireport;

import com.loongtech.bi.manager.report.NotIdfaReportManager;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 运营报表-IOS正版（去积分墙）Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/bi/notIdfaReport")
public class NotIdfaReportBiAction {
    private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间

    private List<NotIdfaReportManager.NotIdfaReportData> platformDataList;  // 平台数据，包括平台汇总和平台详细，其中平台详细是平台汇总里GameReportData对象的togglerDatas属性。平台汇总数据也用于供前端显示

    private List<NotIdfaReportManager.NotIdfaReportData> detailPlatformDisplayData;  // 供前端显示的平台详细数据

	@Autowired
    NotIdfaReportManager notIdfaReportManager;
	
	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
        platformDataList = new ArrayList<>();
        detailPlatformDisplayData = new ArrayList<>();

        Date startDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio) + 1) * Time.DAY))).getTime()));  // 开始时间
        Date endDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY))).getTime()));  // 结束时间

        List<NotIdfaReportManager.NotIdfaReportData> allData = notIdfaReportManager.getAllGameReportData(startDay, endDay);
		if (allData != null && allData.size() > 0) {
            notIdfaReportManager.updateData(allData, platformDataList);
            selectPlatform();
        }
	}
	
    public void selectPlatform() {
        detailPlatformDisplayData.clear();

        for (NotIdfaReportManager.NotIdfaReportData p: platformDataList) {  // 取对应平台的平台详细数据
            detailPlatformDisplayData.addAll(p.getTogglerDatas());
        }
    }

    /**
     * 切换天数
     */
    public void radioChange() {
        int stayDay = "0".equals(dateRadio) ? 2 : Integer.parseInt(dateRadio);
        dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - stayDay * Time.DAY));

        search();
    }

    /**
     * 划动天数
     */
    public void spinnerChange() {
        search();
    }
	
	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

    public List<NotIdfaReportManager.NotIdfaReportData> getPlatformDataList() {
        return platformDataList;
    }

    public void setPlatformDataList(List<NotIdfaReportManager.NotIdfaReportData> platformDataList) {
        this.platformDataList = platformDataList;
    }

    public void setDetailPlatformDisplayData(List<NotIdfaReportManager.NotIdfaReportData> detailPlatformDisplayData) {
        this.detailPlatformDisplayData = detailPlatformDisplayData;
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public List<NotIdfaReportManager.NotIdfaReportData> getDetailPlatformDisplayData() {
        return detailPlatformDisplayData;
    }
}
